/**
 * Copyright 2012 Brigham Young University
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package edu.byu.nlp.pipes;

import java.util.List;

import com.google.common.collect.Lists;



/**
 * @author rah67
 *
 */
public class DataSources {

	private DataSources() { }
	
	private static class ConnectedDataSource<IL, ID, OL, OD> implements DataSource<OL, OD> {

		private final DataSource<IL, ID> src;
		private final Pipe<IL, ID, OL, OD> pipe;
		
		public ConnectedDataSource(DataSource<IL, ID> src, Pipe<IL, ID, OL, OD> pipe) {
			this.src = src;
			this.pipe = pipe;
		}
		
		@Override
		public Iterable<Instance<OL, OD>> getData() {
			return pipe.apply(src.getData());
		}
		
	}
	
	public static <IL, ID, OL, OD> DataSource<OL, OD> connect(DataSource<IL, ID> src, Pipe<IL, ID, OL, OD> pipe) {
		return new ConnectedDataSource<IL, ID, OL, OD>(src, pipe);
	}
	
	public static <L, D> List<Instance<L, D>> cache(DataSource<L, D> source) {
		List<Instance<L, D>> cached = Lists.newArrayList();
		for (Instance<L, D> instance : source.getData()) {
			cached.add(BasicInstance.of(instance.getLabel(), instance.getSource(), instance.getData()));
		}
		return cached;
	}

	private static class IterableDataSource<L, D> implements DataSource<L, D> {

		private final Iterable<Instance<L, D>> it;
		
		public IterableDataSource(Iterable<Instance<L, D>> it) {
			this.it = it;
		}
		
		@Override
		public Iterable<Instance<L, D>> getData() {
			return it;
		}
		
	}
	
	public static <L, D> DataSource<L, D> from(Iterable<Instance<L, D>> it) {
		return new IterableDataSource<L, D>(it);
	}

	/**
	public static <L, W> List<Instance<L, List<W>>> cacheSequenceData(DataSource<L, List<W>> source) {
		List<Instance<L, List<W>>> cached = Lists.newArrayList();
		for (Instance<L, List<W>> instance : source.getData()) {
			
			cached.add(BasicInstance.of(instance.getLabel(), instance.getSource(), instance.getData()));
		}
		return cached;
	}
   **/
}
